

module.exports = function(source) {

    var code = source
    var regx = /Global\.loadImage\(.*\)/g
    var rets = code.match(regx)

    if (rets) {

        for(const ret of rets) {
            let paramsStr = ret.replace("Global.loadImage(", "")
            paramsStr = paramsStr.replace(")", "")

            let params = paramsStr.split(",")
            if (params.length <= 0) {
                continue
            }

            let package = ""
            let imageName = params[0].trim()

            if (params.length > 1) {
                package = params[1].trim()
            }

            let requireStr = ""
            if (package != "") {
                requireStr = "require(\"/src/pages/\"+"+ package +"+\"/resources/images/\" + ("+ imageName +"))"
            } else {
                requireStr = "require(\"/src/resources/images/\" + ("+ imageName +"))"
            }
            
            let findIdx = 0
            while (true) {
                findIdx = code.indexOf(ret, findIdx)
                if (findIdx == -1) {
                    break
                }

                code = code.substring(0, findIdx) + requireStr + code.substring(findIdx + ret.length)
            }
        }
    }
    
    return code
};